package com.example.a66666.yuejian.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a66666.yuejian.Bean.ActivityBean;
import com.example.a66666.yuejian.Bean.LoginBean;
import com.example.a66666.yuejian.Listener.RetrofitListener;
import com.example.a66666.yuejian.Model.UserModel;
import com.example.a66666.yuejian.R;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

public class ActivityDetailActivity extends AppCompatActivity {
    private TextView activityName,activityTime,activityPlace,activityIntro ,activityReleaseTime,goBback,collect;
    private Button signUpActivity;
    private ImageView imageView;
    private UserModel userModel,iscollectusermodel,collectusermodel,cancelCollectUserModel;
    private RetrofitListener<ActivityBean> listener;
    private RetrofitListener<LoginBean> iscollectlistener,collectListener,cancelCollectListener;
    private int uid;
    private int activityID;
    private int isCollected=0;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_detail);
        sp=getSharedPreferences("me", MODE_PRIVATE);
        uid=sp.getInt("userid",6666);
        initView();
        initListener();
        initClick();
        getData();

    }

    public void initView(){
        activityName=(TextView)findViewById(R.id.name);
        imageView=(ImageView)findViewById(R.id.activity_picture);
        activityTime=(TextView)findViewById(R.id.time);
        activityPlace=(TextView)findViewById(R.id.place);
        activityIntro=(TextView)findViewById(R.id.intro);
        activityReleaseTime=(TextView)findViewById(R.id.release);
        collect=(TextView)findViewById(R.id.collect);
        goBback=(TextView)findViewById(R.id.goback);
    }

    public void initListener(){
        listener=new RetrofitListener<ActivityBean>() {
            @Override
            public void onSuccess(ActivityBean activityBean) {
                activityID=activityBean.getAcid();
                activityName.setText(activityBean.getAcname());
                activityTime.setText(activityBean.getAtime());
                activityPlace.setText(activityBean.getPlace());
                activityIntro.setText(activityBean.getAcintro());
                activityReleaseTime.setText(activityBean.getFtime());
                Picasso.with(imageView.getContext()).load("http://10.0.2.2/yuejian/public/static/activitypic/"+activityBean.getApic()).into(imageView);
                iscollectusermodel=new UserModel();
                iscollectusermodel.isCollect(uid,activityID,iscollectlistener);
            }

            @Override
            public void onFail(String msg) {

            }
        };

        iscollectlistener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                  if(loginBean.getFlag()==1){
                      collect.setText("取消收藏");
                      isCollected=1;
                  }
                  else{
                      collect.setText("收藏");
                      isCollected=0;
                  }

            }

            @Override
            public void onFail(String msg) {

            }
        };

        collectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    Toast.makeText(ActivityDetailActivity.this,"收藏成功",Toast.LENGTH_LONG).show();
                    collect.setText("取消收藏");
                    isCollected=1;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        cancelCollectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==0){
                    Toast.makeText(ActivityDetailActivity.this,"取消收藏成功",Toast.LENGTH_LONG).show();
                    collect.setText("收藏");
                    isCollected=0;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
    }

    public void getData(){
        userModel=new UserModel();
        userModel.getActivityDetail(getIntent().getIntExtra("activityID",1),listener);
    }

    public void initClick(){
        goBback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ActivityDetailActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCollected==0){
                    collectusermodel=new UserModel();
                    collectusermodel.collectActivity(uid,activityID,collectListener);
                }
                else{
                    cancelCollectUserModel=new UserModel();
                    cancelCollectUserModel.cancelCollectActivity(uid,activityID,cancelCollectListener);
                }
            }
        });
    }


}
